﻿using System.Linq;
using System.Data.Linq;

namespace UniMana.Data
{
    public static class Software_RequestFormDetailData
    {
        public static IQueryable GetByRequestForm(this Table<Software_RequestFormDetail> nSoftware_RequestFormDetail, int _iRequestFormKey)
        {
            return nSoftware_RequestFormDetail.Where(p => p.Software_RequestFormKey == _iRequestFormKey).OrderBy(p=>p.Priority);
        }

        /// <summary>
        /// Lấy danh sách chi tiết phiếu với mã yêu cầu
        /// </summary>
        /// <param name="nSoftware_RequestFormDetail"></param>
        /// <param name="RequestKey"></param>
        /// <returns></returns>

        public static IQueryable GetListWithRequestKey(this Table<Software_RequestFormDetail> nSoftware_RequestFormDetail,
            int RequestKey = 0)
        {
            var values = from t in nSoftware_RequestFormDetail
                         where t.Software_RequestFormKey == RequestKey
                         select t;
            return values.Distinct();
        }

        /// <summary>
        /// Kiểm tra được phép chỉnh sửa dòng hay không
        /// </summary>
        /// <param name="nSoftware_RequestFormDetail"></param>
        /// <param name="DetailKey"></param>
        /// <returns></returns>

        public static bool ReturnAllowEditRecord(this Table<Software_RequestFormDetail> nSoftware_RequestFormDetail,
        int DetailKey = 0)
        {
            var values = from t in nSoftware_RequestFormDetail
                         where t.Software_RequestFormDetailKey == DetailKey
                         select t;
            if (values.FirstOrDefault().AgreeFinish == null)
                return true;
            else
                if (values.FirstOrDefault().AgreeFinish == false)
                    return true;
                else
                    return false;
        }

        /// <summary>
        /// Lấy ra STT lớn nhất trong danh sách chi tiết công việc
        /// </summary>
        /// <param name="nSoftware_RequestFormDetail"></param>
        /// <param name="SoftwareKey"></param>
        /// <returns></returns>

        public static int ReturnListNumberMax(this Table<Software_RequestFormDetail> nSoftware_RequestFormDetail,
            int SoftwareKey = 0)
        {
            var values = (from t in nSoftware_RequestFormDetail
                          where t.Software_RequestFormKey == SoftwareKey
                          orderby t.ListOfNumbers descending
                          select t).Take(1);
            if (values.Count() > 0)
                return int.Parse(values.FirstOrDefault().ListOfNumbers.ToString());
            else
                return 0;
        }
    }
}
